package com.project.server.estates;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.poi.ExcelUtil;
import com.project.server.domain.estates.TEstatesAssets;
import com.project.server.service.estates.ITEstatesAssetsService;
import com.project.utils.Dom4jUtil;
import com.project.utils.TestJava;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 运营管理应急处理Controller
 *
 * @author project
 * @date 2021-06-28
 */
@RestController
@RequestMapping("/server/assets")
public class TEstatesAssetsController extends BaseController {

    @Resource
    private ITEstatesAssetsService tEstatesAssetsService;

    /**
     * 查询物业管理资产管理表
     */
    @PreAuthorize("@ss.hasPermi('server:assets:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEstatesAssets tEstatesAssets) {
        startPage();
        List<TEstatesAssets> list = tEstatesAssetsService.selectTEstatesAssetsList(tEstatesAssets);
        return getDataTable(list);
    }

    /**
     * 导出物业管理资产管理表
     */
    @PreAuthorize("@ss.hasPermi('server:assets:export')")
    @Log(title = "导出物业管理资产管理表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TEstatesAssets tEstatesAssets) {
        List<TEstatesAssets> list = tEstatesAssetsService.selectTEstatesAssetsList(tEstatesAssets);
        ExcelUtil<TEstatesAssets> util = new ExcelUtil<TEstatesAssets>(TEstatesAssets.class);
        return util.exportExcel(list, "物业资产管理列表");
    }

    /**
     * 查询物业管理资产管理详情
     */
    @PreAuthorize("@ss.hasPermi('server:assets:query')")
    @GetMapping(value = "/{assetsNum}")
    public AjaxResult getInfo(@PathVariable("assetsNum") Long assetsNum) {
        return AjaxResult.success(tEstatesAssetsService.selectTEstatesAssetsByNum(assetsNum));
    }

    /**
     * 新增物业管理资产管理
     */
    @PreAuthorize("@ss.hasPermi('server:assets:add')")
    @Log(title = "新增物业管理资产管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TEstatesAssets tEstatesAssets) {
        return toAjax(tEstatesAssetsService.insertTEstatesAssets(tEstatesAssets));
    }

    /**
     * 修改物业管理资产管理
     */
    @PreAuthorize("@ss.hasPermi('server:assets:edit')")
    @Log(title = "修改物业管理资产管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TEstatesAssets tEstatesAssets) {
        return toAjax(tEstatesAssetsService.updateTEstatesAssets(tEstatesAssets));
    }

    /**
     * 删除物业管理资产管理
     */
    @PreAuthorize("@ss.hasPermi('server:assets:remove')")
    @Log(title = "删除物业管理资产管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{assetsNums}")
    public AjaxResult remove(@PathVariable Long[] assetsNums) {
        return toAjax(tEstatesAssetsService.deleteTEstatesAssetsByNums(assetsNums));
    }

    @Async
    @Log(title = "获取所有设备点位信息", businessType = BusinessType.OTHER)
    @GetMapping("/monitor/queryAll")
    public AjaxResult monitorQuery(String appName) {
        List<TEstatesAssets> arrayList = Dom4jUtil.parseAssetsInfo(TestJava.FILE_PATH);
        List<TEstatesAssets> collect = arrayList.stream().distinct().collect(Collectors.toList());
        return toAjax(tEstatesAssetsService.operateList(collect));
    }
}
